///////////////////////////////////////////////////////////////////
// ENBSeries effect file
// made by : Marty McFly, DKT70
// Feel free to mod it like you want & distribute on your own settings
// As long as you mention everyone's names
// 
//Copyright : Boris Vorontsov/DKT70/Marty McFly
//
//////////////////////////////////////////////////////////////////

// DKT70 HDR
//////////////////////////////////-----------------------------------------------------------------]

#define HDR

float Defog=0.000; // Strength of FogColor, higher = more.
float4 FogColor={0.0, 0.0, 0.0, 0.0}; // Lens-style color filters for Blue, Red, Yellow, White.
float Exposure=0.700; // Contrast settings, higher = brighter, but also more white.
float Gamma=0.250; // Gamma settings for darker or lighter shadows and dark areas, higher = darker.
float BlueShift=0.25; // Shifts entire color spectrum towards blue, good for images too yellow, but this is global.

// DKT70 Sharpen effect
//////////////////////////////////-----------------------------------------------------------------]

float sharps = 0.50; //sharpen strength; always offset value x 2
float offsetv = 0.00; //sharpen offset 
float sxres = 3840; // Horizontal Resolution setting x 2
float syres = 2160; // Vertical Resolution setting x 2
float aspect = 1.6; // horizontal/vertical resolution


// broken, searching for a fix, hopefully PetkaGtA can help me
//////////////////////////////////-----------------------------------------------------------------]

#define BLOOM 

// PetkaGtA bloom
//////////////////////////////////-----------------------------------------------------------------]

#define BLOOM2

#ifdef BLOOM2
static const float thresh = 0.15;
float4 threshhold = {thresh, 1/(1-thresh), 5.0, 5.0};
float BloomIntensity = 5.0;
const float blurSize = 1.0/256.0;
const float BlurDownsampling = 8;
#endif

// Light Scattering ("Godrays")
//////////////////////////////////-----------------------------------------------------------------]

//#define LIGHTSCATTERING

#ifdef LIGHTSCATTERING
float2 ScreenLightPos = {0.5,0.5};
float Density = 1.50;
static int NUM_SAMPLES = 128;
float Weight = 0.3;
float Decay = 1.0;
float SStresh = 0.95;
float SunExposure = 1.0;
#endif

// main color correction by me and openzdl aka AAA (AgainstAllAuthority)
//////////////////////////////////-----------------------------------------------------------------]

//#define COLORCORRMAIN

// Reinhard tonemap. you have to get used to it, very strange look, 
//you have to build up your setting on it
//////////////////////////////////-----------------------------------------------------------------]

//#define REINHARD_ICE21
	#define REINHARD_SAT	 0.7 //Saturation only for Reinhard tonemap
	#define TONEMAPEXP 	 1.4 //Exposure only for Reinhard tonemap
	#define LUM_FACTOR 	float4(0.299, 0.587, 0.114, 0.0)

// simple gamam Correction, lower = darker
//////////////////////////////////-----------------------------------------------------------------]

#define GAMMACORRECTION
	#define GammaAmount       1.5 //Gamma intensity

// the well-known filmiccurve (important: darkens screen really hard by default, I've added
// FilmicExposure which again turns up the brightness to a good amount. Changing the ABCDEFW values
// may require changing the FilmicExposure level
//////////////////////////////////-----------------------------------------------------------------]

//#define FILMICCURVE
	#ifdef FILMICCURVE
		float	FilmicExposure = 7;

		float3 dnsatd = float3( 0.55, 0.53, 0.5 );
		float bchnd = 0.5;
		float hbs = 0.5; 

		float	A = 0.50;
		float	B = 0.50;
		float	C = 0.30;
		float	D = 2.50;
		float	E = 0.02;
		float	F = 1.50;
		float	W = 20.0; // 20

		float3 Uncharted2Tonemap(float3 x)
		{
			return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
		}
	#endif

// HeliosDoubleSix (HD6) Vignette code
//////////////////////////////////-----------------------------------------------------------------]

#define HD6_VIGNETTE
	#define LEFTANDRIGHT //only enable one of the three options here!
	//#define TOPANDBOTTOM //only enable one of the three options here!
	//#define CORNERDARKEN //only enable one of the three options here!
	#ifdef HD6_VIGNETTE
	float   SquareTop = 0.63;              // TOP of Screen.
	float   SquareBottom = 0.63;   	// BOTTOM of Screen.
	float   CircularPower = 0.1;    	// CIRCULAR Vignette Amount.
	float   ColorDistortion = -0.666;  	// COLOR Distortion Strength.
	float   ContrastSharpen = 11.666;       // Contrast & Sharpness Increase.
	float   NightStrength = 6.5;            // Strength at Night of Effect.
	#endif

#define BORISVIGNETTE

#ifdef BORISVIGNETTE
float	EVignetteAmount=3.5;
float	EVignetteCurve=2.5;
float	EVignetteRadius=0.8; //doubled for a little optimization
float3	EVignetteColor=float3(0.0, 0.0, 0.0); //black
#endif

// Cross Processing by AAA
//////////////////////////////////-----------------------------------------------------------------]

//#define CROSSPROCESS
	#ifdef CROSSPROCESS
		float CrossContrast =1.0;
		float CrossSaturation2 = 1.0;
		float CrossBrightness = 0.0;
		float CrossAmount = 0.8;
		float2 crossMatrix [3] =
		 {
			float2 (1.03, 0.04),
			float2 (1.09, 0.01),
			float2 (0.78, 0.13),
		 };
	#endif

// Cross Process by icelaglace 
//////////////////////////////////-----------------------------------------------------------------]

#define CROSSICE
	#ifdef CROSSICE
	float fRatio = 0.55;
	float moodR = 1.0;
	float moodG = 1.1;
	float moodB = 0.5;
	#endif

// HSV-based Color Correction, looks much more difficult as it is :)	
//////////////////////////////////-----------------------------------------------------------------]

//#define HSVULTIMATE
#ifdef HSVULTIMATE
	#define ENABLE_HSV_CONVERSION  	// Enables HSV Conversion. Required for color controls below.
	#define ENABLE_SATURATION_ADJUSTMENT	// Enables color saturation control for the entire scene.
	#define ENABLE_INTENSITY_ADJUSTMENT  // Enables brightness control for the entire scene.
	#define ENABLE_HUE_ADJUSTMENT    	// Enables hue control for the entire scene.
	#define ENABLE_COLOR_EQUALIZER    // Enables the Color Equalizer system for advanced tweaking.

	//#define ENABLE_POW_CURVE    // Enables power curve controls.

	// Color Saturation Variables
	float	EColorSaturationMod=0.0;  // Adds to the overall color saturation. Range: -1 to 1. Default: 0.0
	float	EColorSaturationMult=1.0;  // Multiplies the color saturation. Range: 0.0 (greyscale) to ???. Default: 1.0
	float	EColorSaturationPow=1.0;  // Curves the color saturation. Range: 0.0 to ???. Default: 1.0

	// Color Intensity Variables
	float   EColorIntensityMod=0.0;  	// Adds to the overall brightness. Range: -1 to 1. Default: 0.0
	float   EColorIntensityMult=1.0;  // Multiplies the color intensity. Range: 0.0 (black) to ???. Default: 1.0
	float   EColorIntensityPow=1.0;  	// Curves the color intensity. Range: 0.0 to ???. Default: 1.0

	// Color Hue Variables
	float   EColorHueMod=0.0;    // Adds to the overall color hue (color shifting). Range: -1 to 1. Default: 0.0
	float   EColorHueMult=1.0;    // Multiplies the color hue (color shifting). Range: 0.0 to ???. Default: 1.0
	float	EColorHuePow=1.0;    // Curves the color hue. Range: 0.0 to ???. Default: 1.0.

	// Color Equalizer Variables
	// Adjust modifier to specific saturations. Range: -??? to ???. Default: 0.0
	float	SaturationModRed = 0.0;
	float	SaturationModOrange = 0.0;
	float	SaturationModYellow = 0.0;
	float	SaturationModGreen = 0.0;
	float	SaturationModCyan = 0.0;
	float	SaturationModBlue = 0.0;
	float	SaturationModMagenta = 0.0;

	// Adjust multiplier to specific saturations. Range: 0.0 to ???. Default: 0.0
	float	SaturationMultRed = 0.1;
	float	SaturationMultOrange = -0.2;
	float	SaturationMultYellow = 0.0;
	float	SaturationMultGreen = 0.2;
	float	SaturationMultCyan = -0.1;
	float	SaturationMultBlue = 0.0;
	float	SaturationMultMagenta = 0.1;

	// Adjust curve of specific saturations. Range: -1.0 to ???. Default: 0.0
	float	SaturationPowRed = 0.0;
	float	SaturationPowOrange = 0.0;
	float	SaturationPowYellow = 0.0;
	float	SaturationPowGreen = 0.0;
	float	SaturationPowCyan = 0.0;
	float	SaturationPowBlue = 0.0;
	float	SaturationPowMagenta = 0.0;
#endif

//////////////////////////////////-----------------------------------------------------------------]
//////////////////////////////////////////////
// DO NOT MODIFY ANYTHING BELOW THIS LINE,  //
// UNLESS YOU KNOW HLSL SHADER PROGRAMMING. //
//////////////////////////////////////////////
//////////////////////////////////-----------------------------------------------------------------]


#ifdef HSVULTIMATE
// List of floating point values for hues, used in equalizer.
float	HueRed = 0.0;
float	HueOrange = 0.08333333;
float	HueYellow = 0.16666667;
float	HueGreen = 0.33333333;
float	HueCyan = 0.5;
float	HueBlue = 0.66666667;
float	HueMagenta = 0.83333333;
float	HueRed2 = 1.0;

float	HueSkin = 0.07;
float	HueSky = 0.58;


float ColorEqualizerMult(in float H)
{
float SMult = 1.0;
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMult += SaturationMultOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMult += SaturationMultYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMult += SaturationMultGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMult += SaturationMultCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMult += SaturationMultBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMult += SaturationMultMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMult;
}

float ColorEqualizerMod(in float H)	
{
float SMod = 0.0;
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMod += SaturationModOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMod += SaturationModYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMod += SaturationModGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMod += SaturationModCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMod += SaturationModBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMod += SaturationModMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMod;
}

float ColorEqualizerPow(in float H)	
{
float SPow = 1.0;
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SPow += SaturationPowOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SPow += SaturationPowYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SPow += SaturationPowGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SPow += SaturationPowCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SPow += SaturationPowBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SPow += SaturationPowMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SPow;
}

float3 HUEtoRGB(in float H)
{
   float R = abs(H * 6.0 - 3.0) - 1.0;
   float G = 2.0 - abs(H * 6.0 - 2.0);
   float B = 2.0 - abs(H * 6.0 - 4.0);
   return saturate(float3(R,G,B));
}

float RGBCVtoHUE(in float3 RGB, in float C, in float V)
{
     float3 Delta = (V - RGB) / C;
     Delta.rgb -= Delta.brg;
     Delta.rgb += float3(2.0,4.0,6.0);
     Delta.brg = step(V, RGB) * Delta.brg;
     float H;
     H = max(Delta.r, max(Delta.g, Delta.b));
     return frac(H / 6.0);
}

float3 HSVtoRGB(in float3 HSV)
{
   float3 RGB = HUEtoRGB(HSV.x);
   return ((RGB - 1) * HSV.y + 1) * HSV.z;
}
 
float3 RGBtoHSV(in float3 RGB)
{
   float3 HSV = 0.0;
   HSV.z = max(RGB.r, max(RGB.g, RGB.b));
   float M = min(RGB.r, min(RGB.g, RGB.b));
   float C = HSV.z - M;
   if (C != 0.0)
   {
     HSV.x = RGBCVtoHUE(RGB, C, HSV.z);
     HSV.y = C / HSV.z;
   }
   return HSV;
}
#endif


//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture2D texColor;

//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------


sampler2D InputSampler = sampler_state
{
    Texture = (texColor);
    MinFilter = POINT;
    MagFilter = POINT;
    MipFilter = POINT;
    AddressU   = Clamp;
	AddressV   = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};


struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};


//--------------------------------------------------------------------------------------
// Vertex Shader Input
//--------------------------------------------------------------------------------------

VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------


float Luminance( float3 c )
{
	return dot( c, float3(0.22, 0.707, 0.071) );
}


float4 main(VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{
    float4 color = tex2D(InputSampler, i.txcoord);


#ifdef BLOOM2
float4 sum = float4(0,0,0,0);
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 4.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.05;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 3.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 2.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x, i.txcoord.y,0,BlurDownsampling)) * 0.16;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 2.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 3.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 4.0*blurSize, i.txcoord.y,0,BlurDownsampling)) * 0.05;

float4 bloomcolor = max(float4(0.0,0.0,0.0,0.0), sum-threshhold.x);
float4 toBlend = saturate (bloomcolor * BloomIntensity);
float4 smpl = 1-(1-color)*(1-toBlend);

color = smpl;

#endif


#ifdef HSVULTIMATE
#ifdef ENABLE_HSV_CONVERSION
float3 hsvcolor = RGBtoHSV( color.xyz );

#ifdef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * pow( hsvcolor.x, EColorHuePow ) );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * pow( hsvcolor.y, EColorSaturationPow ) );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * pow( hsvcolor.z, EColorIntensityPow ) );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * pow( hsvcolor.y, ColorEqualizerPow( hsvcolor.x ) ) );
#endif
#endif

#ifndef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * hsvcolor.x );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * hsvcolor.y );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * hsvcolor.z );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * hsvcolor.y );
#endif
#endif

hsvcolor.y = max( hsvcolor.y, 0.0 );
hsvcolor.z = max( hsvcolor.z, 0.0 );
color.xyz = HSVtoRGB( hsvcolor );
#endif
#endif


#ifdef BLOOM
float4 bloom = float4(0,0,0,0);
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(1,0) * 0.001);
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(1,1) * 0.001);	
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(0,1) * 0.001);
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(-1,0) * 0.001);
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(-1,-1) * 0.001);
bloom += 0.2 * tex2Dlod(InputSampler, i.txcoord.xy + float2(0,-1) * 0.001);

bloom *= 0.45;

static const float thresh = 0.15;
float4 threshhold = {thresh, 1/(1-thresh), 5.0, 5.0};
float4 troll = max(float4(0.0,0.0,0.0,0.0), bloom-threshhold.x);
float BloomIntensity = 1.0;
float4 toBlend = saturate (troll * BloomIntensity);

color = color + 1.2 * bloom * 0.1;
#endif

#ifdef GAMMACORRECTION
color = pow(color, 1/GammaAmount);
#endif

#ifdef COLORCORRMAIN
color.r = (color.r)*(0.05 + 1.0f) + (color.g + color.b)*(-0.04); 
color.g = (color.g)*(0.06 + 1.0f) + (color.r + color.b)*(-0.04);
color.b = (color.b)*(0.055 + 1.0f) + (color.r + color.g)*(-0.04);

color.rgb = (color.rgb * (1.0 + color.rgb / 2))/(color.rgb + 4);
color = color*(1.0f + 0.2) - 0.12;
#endif

#ifdef BLOOM
float3 lumCoeff = float3(0.2126, 0.7152, 0.0722);
float luma;
luma = dot(bloom.rgb, lumCoeff);
float3 pwdbloom = pow(bloom.rgb, 0.93); 
float pwdluma = dot(pwdbloom.rgb, lumCoeff);
bloom.rgb = pwdbloom * luma / (0.00001 + pwdluma);
color = 1 - (0.97 - 0.065 * bloom)*(1.04 - color);
#endif

#ifdef COLORCORRMAIN
color = saturate(color);
color = 1 - (0.97 - 0.065)*(1.04 - color);
color = saturate(color);
color = color + (0.82f / 2.0 - 0.5) * (1.0 - color);
color = saturate(color);
color.rgb = pow(0.85f * color, 1.0 / 0.95f); 

float dbr = -color.r + 1.4f;
float dbg = -color.g + 1.4f;
float dbb = -color.b + 1.4f;

float pr = lerp(dbr, 0.55f, 0.66f);
float pg = lerp(dbg, 0.55f, 0.66f);
float pb = lerp(dbb, 0.55f, 0.66f);

color.r = pow((color.r * 0.94f - 0.0035f), pr);
color.g = pow((color.g * 0.94f - 0.002f), pg);
color.b = pow((color.b * 0.91f + 0.001f), pb);

color = color * 4.0;
#endif

#ifdef FILMICCURVE
float3 curr = Uncharted2Tonemap(color);
float3 whiteScale = Uncharted2Tonemap(W);
color.xyz = curr*whiteScale;
color = pow(color, 0.55);
color = color*FilmicExposure;
#endif

#ifdef REINHARD_ICE21
float pixelLumi = dot(color.xyz, LUM_FACTOR.xyz);
float toneMapCalc = pixelLumi / (pixelLumi + 1);
color =  toneMapCalc * pow(color / pixelLumi, REINHARD_SAT);
color = color * TONEMAPEXP;
#endif

#ifdef CROSSPROCESS

		float4 image1 = color;
		float4 image2 = color;
		float gray = dot(float3(0.5,0.5,0.5), image1);

		image1 = lerp (gray, image1,CrossSaturation2);

		image1 = lerp (0.35, image1,CrossContrast);

		image1 +=CrossBrightness;
		image1 *=1.2;

		image2.r = image1.r * crossMatrix[0].x + crossMatrix[0].y;
		image2.g = image1.g * crossMatrix[1].x + crossMatrix[1].y;
		image2.b = image1.b * crossMatrix[2].x + crossMatrix[2].y;

		color = lerp(image1, image2, CrossAmount);

#endif

#ifdef CROSSICE
float Weight;
float4 colInput = color;
float4 colMood = 1.0f;
colMood.r = moodR;
colMood.g = moodG;
colMood.b = moodB;
float fLum = ( colInput.r + colInput.g + colInput.b ) / 3;
colMood = lerp(0, colMood, saturate(fLum * 2.0));
colMood = lerp(colMood, 1, saturate(fLum - 0.5) * 2.0);
float4 colOutput = lerp(colInput, colMood, saturate(fLum * fRatio));
color.rgb=max(0, colOutput);
#endif

#ifdef HDR
color.xyz = max(0, color.xyz - Defog * FogColor.xyz);
color.xyz *= pow(2.0f, Exposure);
color.xyz = pow(color.xyz, Gamma);
float3 trollface = color.xyz * float3(1.05f, 0.97f, 1.27f);
color.xyz = lerp(color.xyz, trollface, BlueShift);
#endif

#ifdef HD6_VIGNETTE

	float rovigpwr = CircularPower; //for a circular vignette
	float2 sqvigpwr = float2( SquareTop, SquareBottom ); // for the top and bottom of the screen
	float vsatstrength = ColorDistortion; // color distortion
	float vignettepow = ContrastSharpen; // increases the contrast and sharpness
	float vstrengthatnight = NightStrength;
 
 	float2 inTex = i.txcoord;
 	float vhnd = 0.5;
 	float4 voriginal = color;
 	float4 vcolor = voriginal;
 	vcolor.xyz=1;
 	inTex -= 0.5; // center
 	inTex.y += 0.01; // offset from the center
 	float vignette = 1.0 - dot( inTex, inTex );
 	vcolor *= pow( vignette, vignettepow );
 
 	float4 rvigtex = vcolor;
 	rvigtex.xyz = pow( vcolor, 1 );
 	rvigtex.xyz = lerp(float3(0.5, 0.5, 0.5), rvigtex.xyz, 2.25); // contrast
 	rvigtex.xyz = lerp(float3(1,1,1),rvigtex.xyz,rovigpwr); // strength of the circular vinetty
 
//darken the top and bottom
 	float4 vigtex = vcolor;
 	vcolor.xyz = float3(1,1,1);

#ifdef LEFTANDRIGHT
 	float3 topv = min((inTex.x+0.5)*2,1.5) * 2; // top
 	float3 botv = min(((0-inTex.x)+0.5)*2,1.5) * 2; // botton
	topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
	vigtex.xyz = (topv)*(botv);
#endif
#ifdef TOPANDBOTTOM
        float3 topv = min((inTex.y+0.5)*2,1.5) * 2; // top
 	float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; // botton
	topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
	vigtex.xyz = (topv)*(botv);
#endif
#ifdef CORNERDARKEN
	float3 rightv = min((inTex.x+0.5)*2,1.5) * 2;
 	float3 leftv = min(((0-inTex.x)+0.5)*2,1.5) * 2; 
        float3 topv = min((inTex.y+0.5)*2,1.5) * 2; 
 	float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; 
 	rightv= lerp(float3(1,1,1), rightv, sqvigpwr.y);
 	leftv= lerp(float3(1,1,1), leftv, sqvigpwr.x);
        topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
 	vigtex.xyz = (topv)*(botv)*(rightv)*(leftv);
#endif
 	
 	
 	//vigtex.xyz = lerp(float3(1,1,1),vigtex.xyz,sqvigpwr); // strength of the top and bottom
 
 // mix the two types of vignettes
 	vigtex.xyz*=rvigtex.xyz;
	vigtex.xyz = lerp(vigtex.xyz,float3(1,1,1),(vhnd-vstrengthatnight*vhnd)); //for a dark screen
 	vigtex.xyz = min(vigtex.xyz,1);
 	vigtex.xyz = max(vigtex.xyz,0);
 	float3 vtintensity = dot(voriginal.xyz, float3(0.2125, 0.7154, 0.0721));
 	color.xyz = lerp(vtintensity, voriginal.xyz, ((((1-(vigtex.xyz*2))+2)-1)*vsatstrength)+1 );
  	color.xyz *= (vigtex.xyz);
#endif

#ifdef BORISVIGNETTE
	float2	uv=(i.txcoord-0.5)*EVignetteRadius;
	float	vignetteold=saturate(dot(uv.xy, uv.xy));
	vignetteold=pow(vignetteold, EVignetteCurve);
	color.xyz=lerp(color.xyz, EVignetteColor, vignetteold*EVignetteAmount);
#endif

color.a = 1.0f;


float2 InputSize = float2(sxres, syres/aspect);
float Amount = sharps;
float2 offset = offsetv / InputSize;
float4 sharpencolor=tex2D(InputSampler, i.txcoord);
sharpencolor += tex2D(InputSampler, i.txcoord - offset) * Amount;
sharpencolor -= tex2D(InputSampler, i.txcoord + offset) * Amount;

return color*sharpencolor;  
    
}

#ifdef LIGHTSCATTERING
float4 PS_ProcessSunShafts(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR 
{	
	float2 texCoord = IN.txcoord;
	// Calculate vector from pixel to light source in screen space.  
    half2 deltaTexCoord = (texCoord - ScreenLightPos.xy);  
    // Divide by number of samples and scale by control factor.  
    deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;  
    // Store initial sample.  
    half3 color = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
	// Set up illumination decay factor.  
	half illuminationDecay = 1.0f;  
	// Evaluate summation from Equation 3 NUM_SAMPLES iterations.  
	for (int i = 0; i < NUM_SAMPLES; i++)  
	{  
		// Step sample location along ray.  
		texCoord -= deltaTexCoord;  
		// Retrieve sample at new location.  
	    half3 sample = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
		// Apply sample attenuation scale/decay factors.  
		sample *= illuminationDecay * Weight;  
		// Accumulate combined color.  
		color += sample;  
		// Update exponential decay factor.  
		illuminationDecay *= Decay;  
	}
	// Output final color with a further scale control factor.  
	return float4( saturate(color*SunExposure) , color.x);
}
#endif

//--------------------------------------------------------------------------------------
// Compiler
//--------------------------------------------------------------------------------------
technique PostProcess
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 main();
	}
#ifdef LIGHTSCATTERING
	pass P1
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessSunShafts();
		AlphaBlendEnable=True;
		SrcBlend = One;
		DestBlend = One;
	}
#endif
}